package com.wms.web.action;

import com.wms.model.Color;
import com.wms.result.ResultMap;
import com.wms.result.ResultTab;
import com.wms.service.ColorService;
import com.wms.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author This
 * @version 1.0
 * @date 2019/7/24 21:41
 */
public class ColorAction extends BaseAction<Color> {

    @Autowired
    private ColorService service;

    @Override
    public void save() throws IOException {
        if (getModel() == null || StringUtils.isEmpty(getModel().getColName())) {
            responseJson(new ResultMap(0, "表单数据不正确！"), new String[]{"data"});
            return;
        }
        if (service.add(getModel())) {
            responseJson(new ResultMap(1, "保存成功！"), new String[]{"data"});
            return;
        }
        responseJson(new ResultMap(0, "产品颜色已存在，请勿重复添加！"), new String[]{"data"});
    }

    @Override
    public void edit() {

    }

    @Override
    public void delete() throws IOException {
        if (getModel().getId() != null && getModel().getId() != 0) {
            service.delete(getModel());
        }
        if (StringUtils.isNotEmpty(ids)) {
            service.deleteAll(ids);
        }
        responseJson(new ResultMap(1, "删除成功！"));
    }

    @Override
    public void showList() throws IOException {
        List<Color> colors = service.findAll();
        if (null != colors) {
            responseJson(new ResultMap(1, "返回成功", colors), new String[]{});
        }
    }

    public void pageList() throws Exception {
        if (!CommonUtils.hasPage(page, limit)) {
            responseJson(CommonUtils.failResult());
            return;
        }
        String colName = getModel().getColName();
        pb.setCurrentPage(page);
        pb.setPageSize(limit);
        if (StringUtils.isNotEmpty(colName)) {
            String condition = " col_name like '%" + colName + "%' ";
            pb.setCondition(condition);
        }
        service.pageQuery(pb);
        ResultTab resultTab = CommonUtils.responseRst(pb);
        responseJson(resultTab);
    }
}
